﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.Views;
using CommunitySystem.Ctrls;
using System.Collections.ObjectModel;

namespace CommunitySystem.PageView
{
    public partial class PublicShowControl :  AsyncCtrl, IReturnListenerc
    {
        private JinShengService.JinShen_MainInfo _mainInfo = null;
        private ObservableCollection<JinShengService.JinShen_PublicShow_Detail> details = null;
        private JinShengService.JinShengServiceClient _client = null;
        private bool CanEditInfo = true;
        private bool CanSubmit = true;
        private bool CanConfirm = false;
        public PublicShowControl(JinShengService.JinShen_MainInfo mainInfo)
        {
            InitializeComponent();
            _mainInfo = mainInfo;
            btn.IsEnabled = false;
            _client = new JinShengService.JinShengServiceClient();
            if (_mainInfo.Status == CommunitySystem.PageView.JinShenConst.STATUS_Promot_Agree)
                CanEditInfo = false;
            _client.GetOrgForHRCompleted += (sender, e) =>
            {
                if (JinShenConst.Administrator.Contains(e.Result))
                {
                    btn.Visibility = System.Windows.Visibility.Collapsed;
                    btnConfirm.Visibility = System.Windows.Visibility.Visible;
                    btnSubmit.Visibility = System.Windows.Visibility.Collapsed;
                    CanEditInfo = false;
                    CanSubmit = false;
                    CanConfirm = true;
                }
                if (_mainInfo.Status == JinShenConst.STATUS_Promot_Agree)
                {
                    CanEditInfo = false;
                    CanSubmit = false;
                    CanConfirm = false;
                    btn.IsEnabled = false;
                    btnConfirm.IsEnabled = false;
                    btnSubmit.IsEnabled = false;
                }
               
                _client.GetJinShen_PublicShow_DetailAsync("MainID=" + _mainInfo.ID);
                
            };
            _client.GetOrgForHRAsync(EHR.Common.Session.LoginUser.LoginName);

            _client.GetJinShen_PublicShow_DetailCompleted += (sender, e) =>
            {
                details = e.Result;                
                _client.GetJinShen_DangGongWei_DetailCompleted += (sender1, e1) =>
                {
                    if (details == null || details.Count <= 0)//新增
                    {
                        foreach (var d in e1.Result)
                        {
                            JinShengService.JinShen_PublicShow_Detail detail = new JinShengService.JinShen_PublicShow_Detail()
                            {
                                EmpID = d.EmpID,
                                EmpName = d.EmpName,
                                MainID = (int)d.MainID,
                            };
                            details.Add(detail);
                        }
                        
                    }
                    else//编辑
                    {
                        btn.IsEnabled = CanSubmit;
                        foreach (var d in e1.Result)
                        {
                            var newDetails = details.Where(c => (int)c.EmpID == d.EmpID).FirstOrDefault();
                            if (newDetails == null)//有新增同意的情况
                            {
                                JinShengService.JinShen_PublicShow_Detail detail = new JinShengService.JinShen_PublicShow_Detail()
                                {
                                    EmpID = d.EmpID,
                                    EmpName = d.EmpName,
                                    MainID = (int)d.MainID,
                                };
                                details.Add(detail);
                                btn.IsEnabled = false;
                            }
                        }
                    }
                    Init();
                };
                _client.GetJinShen_DangGongWei_DetailAsync("MainID=" + _mainInfo.ID + " and IsAgree = '同意'");
                //}
            };
           

            _client.SaveJinShenMainInfoCompleted += (sender, e) =>
            {
                if (e.Error == null && _mainInfo.Status == JinShenConst.STATUS_Candidate_Show)
                {
                    _client.SaveJinShen_PublicShow_DetailAsync(details);
                }
                else if(e.Error == null && _mainInfo.Status == JinShenConst.STATUS_Promot_Apply)
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("申请提交成功！");
                }
                else if (e.Error == null && _mainInfo.Status == JinShenConst.STATUS_Promot_Agree)
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("确认成功!");
                }
            };

            _client.SaveJinShen_PublicShow_DetailCompleted += (sender, e) =>
            {
                if (e.Error == null)
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("公示结果上报成功!");
                    btn.IsEnabled = CanSubmit;
                }
            };

            _client.ComfirmJinShenCompleted += (sender, e) =>
            {
                if (e.Result == true)
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("确认成功");
                    btnConfirm.IsEnabled = false;
                }
                else
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("确认失败");
                }
            };
        }

        public void BeginReturn()
        {
            
        }

        private void Init()
        {
            foreach (var d in details)
            {
                Border b = new Border() { BorderBrush = new SolidColorBrush(Colors.Blue), BorderThickness = new Thickness(1), Margin = new Thickness(5) };
                StackPanel sp = new StackPanel() { Margin = new Thickness(3), DataContext = d };
                b.Child = sp;
                StackPanel sp1 = new StackPanel() { Orientation = Orientation.Horizontal, Margin = new Thickness(3) };
                sp.Children.Add(sp1);

                TextBlock tb = new TextBlock() { Text = "经", Margin = new Thickness(0, 3, 0, 0) };
                sp1.Children.Add(tb);
                DatePicker dp = new DatePicker() { Margin = new Thickness(5, 0, 5, 0) };
                //if (CanEditInfo)
                    dp.IsEnabled = CanEditInfo;
                dp.SetBinding(DatePicker.SelectedDateProperty, new System.Windows.Data.Binding("StartDate") { Mode = System.Windows.Data.BindingMode.TwoWay, StringFormat = "yyyy-MM-dd" });
                sp1.Children.Add(dp);
                tb = new TextBlock() { Text = "至", Margin = new Thickness(0, 3, 0, 0) };
                sp1.Children.Add(tb);
                dp = new DatePicker() { Margin = new Thickness(5, 0, 5, 0) };
                //if (CanEditInfo)
                dp.IsEnabled = CanEditInfo;
                dp.SetBinding(DatePicker.SelectedDateProperty, new System.Windows.Data.Binding("EndDate") { Mode = System.Windows.Data.BindingMode.TwoWay, StringFormat = "yyyy-MM-dd" });
                sp1.Children.Add(dp);

                tb = new TextBlock() { Text = "公示" + d.EmpName + "同志晋升" + _mainInfo.Position + "(" + _mainInfo.PositionLevel + ")", Margin = new Thickness(0, 3, 5, 0) };
                sp1.Children.Add(tb);

                DictComboBox dict = new DictComboBox() { Field = "Condition", DictCode = "HasProblem", DisplayMemberPath = "DictValue", SelectedValuePath = "DictValue", Width = 150 };
                //dict.SetBinding(DictComboBox.SelectedValueProperty, new System.Windows.Data.Binding("Condition"){ Mode = System.Windows.Data.BindingMode.TwoWay});
                sp1.Children.Add(dict);
               // if (CanEditInfo)
                dict.IsEnabled = CanEditInfo;
                StackPanel sp2 = new StackPanel() { Orientation = Orientation.Horizontal, Margin = new Thickness(3), DataContext = d };
                dict.Tag = sp2;
                dict.SelectionChanged += (sender, e) =>
                {
                    var com = sender as DictComboBox;
                    var panel = com.Tag as StackPanel;
                    var item =  dict.SelectedItem as DataService.DataDictionaryItemModel;
                    if (item.DictValue.Equals("有情况反映"))
                    {
                        panel.Visibility = System.Windows.Visibility.Visible;
                    }
                    else
                    {
                        panel.Visibility = System.Windows.Visibility.Collapsed;
                    }
                };
                tb = new TextBlock() { Text = "处理意见如下 : ", Margin = new Thickness(0,3,0,0) };
                sp2.Children.Add(tb);
                TextBox box = new TextBox() { Width = 150};
               // if (CanEditInfo)
                box.IsEnabled = CanEditInfo;
                box.SetBinding(TextBox.TextProperty, new System.Windows.Data.Binding("DSC") { Mode = System.Windows.Data.BindingMode.TwoWay });
                sp2.Children.Add(box);

                sp.Children.Add(sp2);

                spDetails.Children.Add(b);
            }
        }


        /// <summary>
        /// 公示结果上报
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ImageButton_Click(object sender, RoutedEventArgs e)
        {
            _mainInfo.Status = JinShenConst.STATUS_Candidate_Show;
            _client.SaveJinShenMainInfoAsync(_mainInfo);
        }

        /// <summary>
        /// 申请晋升
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Click(object sender, RoutedEventArgs e)
        {
            _mainInfo.Status = JinShenConst.STATUS_Promot_Apply;
            _client.SaveJinShenMainInfoAsync(_mainInfo);
        }

        /// <summary>
        /// 确认晋升
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnConfirm_Click(object sender, RoutedEventArgs e)
        {
            //_mainInfo.Status = JinShenConst.STATUS_Promot_Agree;
            //_client.SaveJinShenMainInfoAsync(_mainInfo);
            SilverlightMessageBoxLibrary.CustomMessage cm = new SilverlightMessageBoxLibrary.CustomMessage("确认同意晋升？", SilverlightMessageBoxLibrary.CustomMessage.MessageType.Confirm, null);
            cm.Show();
            cm.OKButton.Click -= new RoutedEventHandler(OKButton_Click);
            cm.OKButton.Click += new RoutedEventHandler(OKButton_Click);
        }

        void OKButton_Click(object sender, RoutedEventArgs e)
        {
            _client.ComfirmJinShenAsync(_mainInfo.ID, JinShenConst.STATUS_Promot_Agree);
        }

        
    }
}
